package com.copm.ifm.servers.sys.controller;

import com.copm.ifm.base.api.pojo.PageDTO;
import com.copm.ifm.base.api.pojo.PageVO;
import com.copm.ifm.base.basic.ServiceResponse;
import com.copm.ifm.base.basic.exception.ServiceException;
import com.copm.ifm.base.basic.msg.ExceptionMsg;
import com.copm.ifm.base.service.base.BaseController;
import com.copm.ifm.servers.sys.client.SysBrandClient;
import com.copm.ifm.servers.sys.dto.SysBrandPageDTO;
import com.copm.ifm.servers.sys.entity.SysBrand;
import com.copm.ifm.servers.sys.service.SysBrandService;
import com.copm.ifm.servers.sys.vo.SysBrandPageVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * <p>
 * 品牌表 服务类
 * </p>
 *
 * @author tp
 * @since 2020-12-16
 */
@Slf4j
@Api(tags = "品牌/sys-brand")
@RestController
@RequestMapping("/sys-brand")
public class SysBrandController extends BaseController implements SysBrandClient {

    @Resource
    SysBrandService sysBrandService;

    @Override
    @ApiOperation("分页")
    @PostMapping("/page")
    public ServiceResponse<PageVO<SysBrandPageVO>> page(@RequestBody PageDTO<SysBrandPageDTO> param) {
        PageVO<SysBrandPageVO> result = sysBrandService.selectForPage(param);
        return success(result);
    }

    @Override
    @ApiOperation("查询-根据id数据")
    @GetMapping("/by/id")
    public ServiceResponse<SysBrandPageVO> getById(@ApiParam(value = "主键id", required = true)
                                                   @RequestParam int id) {
        return success(sysBrandService.selectVoById(id));
    }

    @Override
    @ApiOperation("保存-品牌save")
    @PostMapping("/save")
    public ServiceResponse<SysBrand> save(SysBrand entity) {
        if (sysBrandService.checkCode(entity.getName())) {
            sysBrandService.save(entity);
            return success(entity);
        } else {
            return fail(ServiceException.parseInfo(ExceptionMsg.ADD_ALREADY_EXISTS,entity.getName()));
        }
    }

    @ApiOperation("修改-品牌update")
    @PutMapping
    public ServiceResponse<SysBrand> update(@RequestBody SysBrand entity) {
        sysBrandService.updateEntityById(entity);
        return success(entity);
    }

    @ApiOperation("删除-根据id")
    @DeleteMapping("/by/id")
    public ServiceResponse<?> deleteById(@ApiParam(value = "主键id", required = true) @RequestParam int id) {
        //修改delete字段为1的删除方式
        sysBrandService.updateDeleted(id);
        return success();
    }

}

